今天接續昨天的貪吃蛇遊戲,在舌頭碰到身體時即遊戲結束,並利用
剛剛有在頭的方向的func裡有提到,就是當頭往下個點移動的時候就會進行一次判斷,看看頭有沒有撞到身體,有的話就執行以下程式碼
//把蛇陣列裡面的每個點都賦予引力和碰撞,再加上重力加速度,所有的物體自然會往下掉
for i in 1..<snakePoint.count{
if self.snakePoint[0].center == tempPoint[i]{
ani = UIDynamicAnimator(referenceView: mainView.secondView)
let gravity = UIGravityBehavior(items: snakePoint) //引力
let collision = UICollisionBehavior(items: snakePoint) //碰撞
collision.translatesReferenceBoundsIntoBoundary = true //在邊界停止
gravity.gravityDirection = CGVector (dx: 0.0, dy: 1.0) //重力加速度的方向
let itemBehavior = UIDynamicItemBehavior(items: snakePoint)
itemBehavior.angularResistance = 0 //碰到牆旋轉速
itemBehavior.elasticity = 1.0 //反彈力
ani.addBehavior(itemBehavior)
ani.addBehavior(collision)
ani.addBehavior(gravity)
timer?.invalidate()
viewSlideUp()
}
}
首先需要在Main.storyBoard裡面的MainVC拉一個View進去上面那排狀態列,就會跑出一個小View在ViewController上面,如下圖
再來記得把view和裡面的按鈕拉OutLet進來,以供UI設計使用,之後記得把這個滑出的動畫的程式碼放在ViewWillAppear裡面,才不會跑不出畫面來
override func viewWillAppear(_ animated: Bool) {
let height = appearView.frame.height
mainView.addSubview(appearView)
appearView.translatesAutoresizingMaskIntoConstraints = false
//以下三行為設定view的約束
appearView.heightAnchor.constraint(equalToConstant: height).isActive = true
appearView.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 10).isActive = true
appearView.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: -10).isActive = true
//把底部的約束當作一個id來用,用來當作判斷是否有呼叫到此id,有的話就讓底部的約束改變這時就會進入ViewSlideUp的方法裡
let hide = appearView.bottomAnchor.constraint(equalTo: mainView.bottomAnchor, constant: height)
hide.identifier = "hide"
hide.isActive = true
resetBtn.layer.cornerRadius = 10
endBtn.layer.cornerRadius = 10
appearView.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0)
super.viewWillAppear(animated)
}
當頭碰到身體死掉的時候就會呼叫下面func,當找到id為hide的時候就讓約束改變,並讓view跑上來主要的mainView上,再限制view跑1.25秒才會到指定位置上
func viewSlideUp(){
for i in mainView.constraints{
if i.identifier == "hide"{
let showHeight = (mainView.frame.height - appearView.frame.height)/2
i.constant = -showHeight
break
}
}
UIView.animate(withDuration: 1.25) {
self.mainView.layoutIfNeeded()
}
}
最後就是當按下重新遊戲的時候,再讓view回到原始的位置上
func tapViewBtn(){
for i in mainView.constraints{
if i.identifier == "hide"{
let showHeight = (mainView.frame.height - appearView.frame.height)/2
i.constant = showHeight
break
}
}
UIView.animate(withDuration: 1.0) {
self.mainView.layoutIfNeeded()
}
}